import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.PixelWriter;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class homework_04 extends Application {

    // DDA 算法绘制直线方法
    private void drawLineDDA(GraphicsContext gc, int x1, int y1, int x2, int y2, boolean isDashed, Color color) {
        int dx = x2 - x1;
        int dy = y2 - y1;
        int steps;

        if (Math.abs(dx) > Math.abs(dy)) {
            steps = Math.abs(dx);
        } else {
            steps = Math.abs(dy);
        }

        double xIncrement = (double) dx / (double) steps;
        double yIncrement = (double) dy / (double) steps;
        double x = x1;
        double y = y1;

        // 控制虚线的间隔
        int dashLength = 5;

        for (int i = 0; i <= steps; i++) {
            if (!isDashed || (i % (2 * dashLength) < dashLength)) {
                gc.getPixelWriter().setColor((int) Math.round(x), (int) Math.round(y), color);
            }
            x += xIncrement;
            y += yIncrement;
        }
    }

    // 中点画线算法
    private void drawLineMidPoint(PixelWriter pixelWriter, int x1, int y1, int x2, int y2) {
        int dx = x2 - x1;
        int dy = y2 - y1;
        int d = 2 * dy - dx;
        int incrE = 2 * dy;
        int incrNE = 2 * (dy - dx);
        int x = x1;
        int y = y1;

        pixelWriter.setColor(x, y, Color.BLACK);

        while (x < x2) {
            if (d <= 0) {
                d += incrE;
                x++;
            } else {
                d += incrNE;
                x++;
                y++;
            }
            pixelWriter.setColor(x, y, Color.BLACK);
        }
    }

    // Bresenham 算法
    private void drawLineBresenham(PixelWriter pixelWriter, int x1, int y1, int x2, int y2) {
        int dx = Math.abs(x2 - x1);
        int dy = Math.abs(y2 - y1);
        int sx = x1 < x2 ? 1 : -1;
        int sy = y1 < y2 ? 1 : -1;
        int err = dx - dy;

        int x = x1;
        int y = y1;

        while (true) {
            pixelWriter.setColor(x, y, Color.BLACK);

            if (x == x2 && y == y2) {
                break;
            }
            int e2 = 2 * err;
            if (e2 > -dy) {
                err -= dy;
                x += sx;
            }
            if (e2 < dx) {
                err += dx;
                y += sy;
            }
        }
    }

    // 绘制正六边形的方法
    private void drawHexagon(GraphicsContext gc, int centerX, int centerY, int radius, boolean isDashed, Color color) {
        double angleStep = 2 * Math.PI / 6;
        for (int i = 0; i < 6; i++) {
            double startAngle = i * angleStep;
            double endAngle = (i + 1) * angleStep;

            int startX = (int) (centerX + radius * Math.cos(startAngle));
            int startY = (int) (centerY + radius * Math.sin(startAngle));
            int endX = (int) (centerX + radius * Math.cos(endAngle));
            int endY = (int) (centerY + radius * Math.sin(endAngle));

            drawLineDDA(gc, startX, startY, endX, endY, isDashed, color);
        }
    }

    @Override
    public void start(Stage primaryStage) {
        int width = 800;
        int height = 800;
        Canvas canvas = new Canvas(width, height);
        GraphicsContext gc = canvas.getGraphicsContext2D();

        int hexagonRadius = 30;
        int horizontalSpacing = (int) (hexagonRadius * 1.5);
        int verticalSpacing = (int) (Math.sqrt(3) * hexagonRadius);

        // 定义颜色数组
        Color[] colors = {Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, Color.PURPLE, Color.CYAN, Color.YELLOW, Color.MAGENTA};

        // 这里控制每行和每列六边形的数量
        int numRows = 10;
        int numCols = 10;

        for (int row = 0; row < numRows; row++) {
            for (int col = 0; col < numCols; col++) {
                int centerX = (int) (col * horizontalSpacing + (row % 2 == 1 ? horizontalSpacing / 2 : 0));
                int centerY = (int) (row * verticalSpacing);

                // 让图案居中
                centerX += (width - numCols * horizontalSpacing) / 2;
                centerY += (height - numRows * verticalSpacing) / 2;

                // 计算颜色索引
                int colorIndex = (row * numCols + col) % colors.length;
                Color currentColor = colors[colorIndex];

                drawHexagon(gc, centerX, centerY, hexagonRadius, false, currentColor);
            }
        }

        StackPane root = new StackPane(canvas);
        Scene scene = new Scene(root, width, height);
        primaryStage.setTitle("Honeycomb Pattern");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}